package com.rp.common.utils;

import java.text.SimpleDateFormat;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;

public class TimeHWUtil {
    /***
     *
     * @param date
     * @param dateFormat : e.g:yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String formatDateByPattern(Date date, String dateFormat){
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
        String formatTimeStr = null;
        if (date != null) {
            formatTimeStr = sdf.format(date);
        }
        return formatTimeStr;
    }
    /***
     * convert Date to cron ,eg.  "0 07 10 15 1 ? 2016"
     * @param date  : 时间点
     * @return
     */
    public static String getCron(Date  date){
        //  秒分时日月周年
        String dateFormat= String.format("ss mm HH dd MM %d yyyy",getDay());
        return formatDateByPattern(date, dateFormat);
    }
    //获取周几的一个方法
    /***
     *
     * @describe 获取周几的一个方法
     * @return Integer
     * @author tong
     */
    public static Integer getDay() {
        DayOfWeek dayOfWeek = LocalDateTime.now().getDayOfWeek();
        switch (dayOfWeek){
            case MONDAY:return 1;
            case TUESDAY:return 2;
            case WEDNESDAY:return 3;
            case THURSDAY:return 4;
            case FRIDAY:return 5;
            case SATURDAY:return 6;
            case SUNDAY:return 7;
            default:return 65534;
        }
    }
    //传入两个cron表达式,判断当前时间是否在表达式所代表的时间之内,如果在则返回true
    //暂时只有周期,没有日期
    /***
     *
     * @param startTime ：
     * @param stopTime : e.g:yyyy-MM-dd HH:mm:ss
     * @return boolean
     * @author tong
     */
    public static Boolean isBetween(String startTime, String stopTime) {
        //首先比较起始时间和结束时间i,天和周,
        String[] start = startTime.trim().split(" ");
        String[] stop = stopTime.trim().split(" ");
        ArrayList<LocalDateTime> startDate = new ArrayList<>();
        ArrayList<LocalDateTime> stopDate = new ArrayList<>();
        String[] weeks = start[5].trim().split(",");
        for (int i = 0; i < weeks.length; i++) {
            LocalDateTime dateTime = LocalDateTime.of(2021, 10, Integer.parseInt(weeks[i]), Integer.parseInt(start[2]), Integer.parseInt(start[1]), Integer.parseInt(start[0]));
            startDate.add(dateTime);

        }
        String[] week1s = stop[5].trim().split(",");
        //将周期变为日期进行比较
        for (int i = 0; i < week1s.length; i++) {
            LocalDateTime dateTime = LocalDateTime.of(2021, 10, Integer.parseInt(weeks[i]), Integer.parseInt(stop[2]), Integer.parseInt(stop[1]), Integer.parseInt(stop[0]));
            stopDate.add(dateTime);
        }
        LocalDate localDate = LocalDate.of(2021, 10, TimeHWUtil.getDay());
        LocalTime localTime = LocalTime.now();
        LocalDateTime now = LocalDateTime.of(localDate, localTime);
        for (int i = 0; i < startDate.size(); i++) {
            if (now.isAfter(startDate.get(i)) && now.isBefore(stopDate.get(i))) {
                return true;
            }
        }
        return false;
    }
}

